<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use App\Models\User;
use App\Models\GoldRound;
use App\Models\GoldRecord;
use App\Models\GoldProduct;
use Brick\Math\RoundingMode;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Illuminate\Database\Eloquent\Collection;
use App\Services\MakeGoldRecord\MakeGoldRecord;
use App\Services\CloseUnpaidStorm\CloseUnpaidStorm;
use App\Jobs\MakeGoldRecordJob;
use App\Jobs\ApplyGoldProductPayingJob;
use App\Events\GoldRecordPaidEvent;


class Test extends Command
{
    protected $signature = 'cc:test';

    protected $description = "";

    public function handle()
    {

        /* User::create([
            'grade' => 'vip',
            'name' => 'test',
            'password' => bcrypt('000000'),
            'phone' => '18888888888',
            'invite_code' => '1122334455',
            'gold_beans' => 10000000,
            'gold_coins' => 10000000,
            'avatar' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTI3TZ4PXGvNIevwkpiaaRaQtxw3enBNJjCK0pBkUEFW0IjiaiaHs381tWqfjJIiaibbLmWobwT3rweoYNQ/132',
            'gender' => 'male',
            'wm_openid' => '',
            'wm_session_key' => '',
            'wx_unionid' => '',
            'alipay_user_id' => ''
        ]);*/

        /*$uid = 100;
        $pid = 64;
        $rid = 1;
    
      
        $newCacheId = "100_20231113_1_64";

        $round = 1;
        $allRounds = 19;

        $data = [
            'cid' => $newCacheId,
            'user_id' => $uid,
            'user_name' => '',
            'user_avatar_url' => '',
            'user_phone' => '',
            'gold_round_id' => $rid,
            'gold_product_id' => $pid,
            'gold_record_id' => 0,
            'record_all_price' => '215.89',
            'pay_net_price' => '215.89',
            //'pay_cash_price' => Redis::hGet($cachedPid, 'cash_price_today'),
            'pay_gold_beans' => '12.95',
            'gold_product_is_last_round' => $round >= $allRounds,
            'gold_product_all_price_next' => '265.56',
            'product_title' => '中国金行足金镶天然翡翠金貔宝坠貔貅吊坠',
            'product_cover' => 'https://littleshop.oss-cn-beijing.aliyuncs.com/uploads/images/202309/23/73a7da78c2709433ce57be50fd2800bc.jpg',
            'product_market_price' => '1388.00',
            'to_resell_applied_at' => null,
            'to_split_applied_at' => null,
            'to_take_applied_at' => null,
            'finished_at' => null,
            'paid_at' => null,
            'has_applied_to_resell' => false,
            'has_applied_to_split' => false,
            'has_applied_to_take' => false,
            'splits_all_count' => null,
            'splits_all_money' => null,
            'is_paying' => false,
            'is_paid' => false,
            'is_record_making' => true,
            'is_record_made' => false,
            'is_resell_applying' => false,
            'is_split_applying' => false,
            'is_take_applying' => false,
            'pending_voucher_num' => 0,
            'examine_voucher_num' => 0,
            'is_ending' => false,
            'created_at' => Carbon::now()->toDateTimeString(),
            'resell_user_id' =>  0,
            'resell_user_name' =>  '',
            'resell_user_avatar' =>  '',
            'resell_record_cache_id' => '',
            'paid_expire_at' => Carbon::now()->addMinutes(sc('pay_gold_record_must_within_minutes'))->toDateTimeString(),
        ];

        //Redis::zAdd(self::sUnpaidRecordsCacheKey($uid), 1, $newCacheId);
        Redis::hMset($newCacheId, $data);

        $queue = app()->environment('local') ? 'default' : 'default';
        dispatch(new MakeGoldRecordJob($uid, $newCacheId))->onQueue($queue);*/

       /* $user = User::find(100);
       // $user->addRecordToPendingCache('100_20231113_1_64');
        $recordId = Redis::hGet('100_20231113_1_64', 'gold_record_id');
        
        if ($recordId <= 0) return;
        
        if ($record = GoldRecord::find($recordId)) {
            $record->update(['paid_at' => Carbon::now(), 'paid_method' => 'goldcoin']);

            event(new GoldRecordPaidEvent($record));
        }*/

        /*$user = User::find(11);
         $user->addRecordToFinishedCache("11_20231114_2_12");
        $record = GoldRecord::query()->where("cache_id","11_20231114_2_12")->first();
        $record->update(['finished_at'=>Carbon::now()]);*/


        User::query()
            //->whereHas('goldCharges')
            ->chunks(50, function (Collection $users) {

                if ($users->isEmpty()) return true;

                /**
                 * @var User $user
                 */
                foreach ($users as $user) {
                    if($user->cert_number){
                          $user->bankCards()->create([
                            'bank_abbr' => $user->bank_abbr,
                            'bank_name' => $user->bank_name,
                            'cert_number' => $user->cert_number,
                            'real_name' => $user->real_name,
                            'method' => 'bank',
                            'alipay_real_name' => "",
                            'alipay_number' => "",
                            'is_default' => 1,
                        ]);

                    }   
                  

                    if($user->alipay_number){

                         $user->bankCards()->create([
                            'bank_abbr' => "",
                            'bank_name' => "",
                            'cert_number' => "",
                            'real_name' => "",
                            'method' => 'alipay',
                            'alipay_real_name' => $user->alipay_real_name,
                            'alipay_number' => $user->alipay_number,
                            'is_default' => 1,
                        ]);
                    }
                    
                }

                return true;

            });
    }
}
